//
//  ANMineInfoController.m
//  WeiYiMei
//
//  Created by iMac on 2019/4/13.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANMineInfoController.h"
#import "inputCell.h"
#import "MineInfoFooterView.h"
#import "ANVIPRegisterController.h"

@interface ANMineInfoController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) MineInfoFooterView *footer;

@property (nonatomic, copy) NSString *gender;

@end

@implementation ANMineInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"个人信息";
    
    
    
    [self requsetControl];
}

- (void)requsetControl
{
    [URLManager.shareManager getUrlStr:Api_App_Control parameters:nil success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        
        if (responseObj) {
            if ([responseObj[@"code"] intValue] == 200) {
                NSNumber *data = responseObj[@"data"];
                if (data.boolValue) {
                    if (!self.userInfoEntity.memberKind || self.userInfoEntity.memberKind.intValue != 2) {
                        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"成为正式会员" style:UIBarButtonItemStylePlain target:self action:@selector(addVip)];
                    }
                }
            }
        }
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

- (void)addVip
{
    ANVIPRegisterController *vc = [[ANVIPRegisterController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)editInfo
{
    NSDictionary *body = [self.userInfoEntity yy_modelToJSONObject];
    [URLManager.shareManager postUrlStr:Api_Edit_User_Vip_Info parameters:body success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        if (responseObj) {
            if ([responseObj[@"code"] intValue] == 200) {
                [MBProgressHUD showMessage:responseObj[@"message"]];
                [self.tableView reloadData];
            }
            else {
                [MBProgressHUD showError:responseObj[@"message"]];
            }
        }
        
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        
    }];
}

- (void)setupSubViews
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.footer = [[MineInfoFooterView alloc] initWithFrame:CGRectMake(0, 0, 0, kWidth(100))];
    self.footer.title = @"保存";
    self.tableView.tableFooterView = self.footer;
    WeakSelf
    // 修改信息
    self.footer.sureHandler = ^{
        [weakSelf editInfo];
    };
}

#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    }
    if (section == 1) {
        return 3;
    }
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    inputCell *cell = [tableView dequeueReusableInputCellWithIdentifier:@"inputCell"];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"姓名";
            cell.text = UserUtils.shared.nameString;
            cell.editable = NO;
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"手机号码";
            cell.text = UserUtils.shared.telString;
            cell.editable = NO;
        }
        if (indexPath.row == 2) {
            cell.titleLab.text = @"性别";
            cell.editable = NO;
            cell.text = @"请选择性别";
//            if (self.gender) {
//                if ([self.gender isEqualToString:@"1"]) {
//                    cell.text = @"男";
//                }
//                else {
//                    cell.text = @"女";
//                }
//            }
            if ([self.userInfoEntity.gender isEqualToString:@"1"]) {
                cell.text = @"男";
            }
            else {
                cell.text = @"女";
            }
        }
        if (indexPath.row == 3) {
            cell.titleLab.text = @"家庭住址";
            cell.placeHolderString = @"请输入家庭住址";
            cell.text = StringNone(self.userInfoEntity.address);
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"身份证号";
            cell.placeHolderString = @"请输入身份证号码";
            cell.text = StringNone(self.userInfoEntity.idNumber);
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"银行卡号";
            cell.placeHolderString = @"请输入银行卡号";
            cell.text = StringNone(self.userInfoEntity.bankAccount);
        }
        if (indexPath.row == 2) {
            cell.titleLab.text = @"所属银行";
            cell.placeHolderString = @"请输入所属银行";
            cell.text = StringNone(self.userInfoEntity.bank);
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell.titleLab.text = @"车牌号";
            cell.placeHolderString = @"请输入车牌号码";
            cell.text = StringNone(self.userInfoEntity.carNumber);
            cell.editable = self.userInfoEntity.carNumber.length == 0 ? YES : NO;
            
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"车辆品牌";
            cell.placeHolderString = @"请输入车辆品牌";
            cell.text = StringNone(self.userInfoEntity.carBrand);
            cell.editable = self.userInfoEntity.carBrand.length == 0 ? YES : NO;
        }
        if (indexPath.row == 2) {
            cell.titleLab.text = @"车辆颜色";
            cell.placeHolderString = @"请输入车辆颜色";
            cell.text = StringNone(self.userInfoEntity.carColour);
            cell.editable = self.userInfoEntity.carColour.length == 0 ? YES : NO;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectTextViewAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 2) {
        LLAlertViewController *vc = [[LLAlertViewController alloc] init];
        vc.actions = @[@"男",@"女"];
        vc.actionBlock = ^(NSInteger index, NSString *actionTitle) {
            if (index == 0) {
                self.userInfoEntity.gender = @"1";
            }
            else {
                self.userInfoEntity.gender = @"2";
            }
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        };
        [vc presentBottomViewControllerFromController:self];
    }
}

- (void)tableView:(UITableView *)tableView updataText:(NSString *)text indexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 3) {
            self.userInfoEntity.address = text;
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            self.userInfoEntity.idNumber = text;
        }
        if (indexPath.row == 1) {
            self.userInfoEntity.bankAccount = text;
        }
        if (indexPath.row == 2) {
            self.userInfoEntity.bank = text;
        }
    }
    if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            self.userInfoEntity.carNumber = text;
        }
        if (indexPath.row == 1) {
            self.userInfoEntity.carBrand = text;
        }
        if (indexPath.row == 2) {
            self.userInfoEntity.carColour = text;
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 50;
    }
    return _tableView;
}

@end
